<?php $title = '登录'; require './header.php'; ?>

<div class="container-fluid">
	<div class="card">
		<div class="card-header">登录</div>
		<div class="card-body">
			<div class="input-group mb-3">
				<div class="input-group-prepend">
					<span class="input-group-text">手机号</span>
				</div>
				<input type="text" class="form-control" placeholder="请输入你的手机号码" id="phone">
			</div>
			<div class="input-group mb-3">
				<div class="input-group-prepend">
					<span class="input-group-text">密　码</span>
				</div>
				<input type="password" class="form-control" placeholder="请输入密码" id="password">
			</div>
			<div class="form-check mb-3">
				<label class="form-check-label">
					<input type="checkbox" class="form-check-input" id="agree">
					已阅读并同意<a href="https://www.huluxia.com/protocol/protocol_users.html">《葫芦侠用户及服务协议》</a>与<a href="https://www.huluxia.com/protocol/protection_guidelines.html">《隐私政策》</a>
				</label>
				<span class="float-right">忘记密码了？<a href="./forget.php">找回密码</a></span>
			</div>
			<button type="button" class="btn btn-primary btn-block" id="login">登录</button>
		</div> 
	</div>
</div>

<script>
$('#login').click(function() {
	var phone = $('#phone').val();
	var password = $('#password').val();
	var agree = $('#agree').is(':checked');
	
	if(! phone) $('#phone').focus();
	else if(! password) $('#password').focus();
	else if(! agree) $('#agree').parent().tooltip({title:'请勾选','placement':'right'}).tooltip('show');
	else {
		$.post('./ajax/login.php',{phone:phone,password:password},function(data) {
			data = JSON.parse(data);
			if(data.status) {
				alert('欢迎 ' + data.user.nick + ' 登录葫芦侠Web版！');
				setCookie('key',data._key,30 * 3600 * 24);
				window.location.href = '<?php echo $_REQUEST['ref_url']; ?>' ? '<?php echo $_REQUEST['ref_url']; ?>' : './index.php';
			} else {
				alert(data.msg);
			}
		});
	}
});
</script>
</body>
</html>